package com.example.demo.Service;

import com.example.demo.Dao.BookInfoDao;
import com.example.demo.enums.BookStatusEnums;
import com.example.demo.model.BookInfo;
import com.example.demo.model.ListInfo;
import com.example.demo.model.ListInfoMoke;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class BookService {
    @Autowired
    public BookInfoDao bookInfoDao;
    public ListInfo<BookInfo> getLists(ListInfoMoke listInfoMoke){

        Integer currentPage = (listInfoMoke.getCurrentPage()-1)*10;
        Integer pageSize = listInfoMoke.getPageSize();
       Integer total =  bookInfoDao.selectTotal();
        List<BookInfo> list = new ArrayList<>();
      list =  bookInfoDao.selectAll(currentPage,pageSize);

      if(list==null || list.size()==0){
          return null;
      }else{
          for (BookInfo bookInfo:list){
           bookInfo.setStatusCN(BookStatusEnums.getInformationByStatus(bookInfo.getStatus()).getStatusCN());
          }
      }
       ListInfo<BookInfo> listInfo = new ListInfo<>();
       listInfo.setBookInfos(list);
       listInfo.setTotal(total);
       listInfo.setListInfoMoke(listInfoMoke);
       return  listInfo;
    }

    public Integer insertDate(BookInfo bookInfo){
       int flag =  bookInfoDao.insertDate(bookInfo);
     return flag;
    }

    public BookInfo selectBookById(Integer id){

       BookInfo bookInfo = new BookInfo();
       bookInfo = bookInfoDao.selectById(id);
       return bookInfo;
    }

    public Boolean updateBook(BookInfo bookInfo){
      int flag = bookInfoDao.updateBook(bookInfo);
        if(flag==1){
            return true;
        }
        return false;
    }

    public Boolean deleteBook(Integer id){
       int flag =  bookInfoDao.deleteBook(id);
       if(flag==1){
           return true;
       }
       return false;
    }

    public Boolean deleteBooks(List<Integer> ids){
        int flag = bookInfoDao.deleteBooks(ids);
        if(flag==1){
            return true;
        }
        return false;
    }
}
